var webroot = document.location.href;
webroot = webroot.substring(webroot.indexOf('//') + 2, webroot.length);
webroot = webroot.substring(webroot.indexOf('/') + 1, webroot.length);
webroot = webroot.substring(0, webroot.indexOf('/'));
var rootpath = '/' + webroot;


//获得弹出窗口的参数
function getWindowOpenArg(width, height){
    var topPX = (screen.height - height)/2;
    var leftPX = (screen.width - width)/2;
    var argStr = "top=" + topPX + ",left=" + leftPX + ",width=" + width + ",height=" + height;
    argStr = argStr + ",menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no";
    return argStr;
}

//增加一个名为 trim 的函数作为String构造函数的原型对象的一个方法。 
String.prototype.trim = function(){ 
    //用正则表达式将前后空格,用空字符串替代 
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

//关闭弹出窗口
function closeChildWindow(childWindow){
    if(childWindow != null && childWindow) childWindow.close();
}

//回车码转换为tab码，推动焦点下移
function turnNext(object) {
    if (object.keyCode == 13) {
        object.keyCode = 9
    }
}

//设置所有的button按钮失效
function setAllDisabled(obj) {
    for (var i = 0; i < obj.all.length; i++) {
        if (obj.all[i].type == "button") {
            obj.all[i].disabled = true;
        }
    }
}
function setAllEnable(obj){
    for (var i = 0; i < obj.all.length; i++) {
        if (obj.all[i].type == "button") {
            obj.all[i].disabled = false;
        }
    }
}


//检查是否为空
function checkIsNotNull(element,message){
    var name=document.getElementById(element).value;
    if(name==null||name.trim()==""){
    	alert(message);
    	document.getElementById(element).focus();
    	return false;
    }
    else{
    	return true;
    }
 }

//检查是否做出选择, 未选择参数为0
function checkIsNotSelect(element,message){
    var name=document.getElementById(element).value;
    if(name==null||name==0){
    	alert(message);
    	document.getElementById(element).focus();
    	return false;
    }
    else{
    	return true;
    }
 }

//移动多选下拉列表框选项
function moveMultiSelectOption(fromSelectName, toSelectName, fromSelectCue){
    var fromSelect=document.getElementById(fromSelectName);
    var toSelect=document.getElementById(toSelectName);
    if(fromSelectCue != null && fromSelect.selectedIndex==-1){
      alert("请选择" + fromSelectCue + ".");
      return;
    }
    while(fromSelect.selectedIndex!=-1){
	  option=fromSelect.options[fromSelect.selectedIndex];
	  fromSelect.remove(fromSelect.selectedIndex);
	  toSelect.options.add(new Option(option.text, option.value));
    }
}

//选择多选下拉列表框选项
function setMultiSelectOption(selectName){
    var obj=document.getElementById(selectName);
    for(i=0;i<obj.length;i++){
        obj[i].selected=true;
    }
}

//全选
function selectAll(element1,element2){
	for(var i=0;i<document.getElementsByName(element1).length;i++)
		{
			document.getElementsByName(element1)[i].checked=document.getElementsByName(element2)[0].checked
		}
}

//如果有一个未被选中 取消全选, 如果全部选中, 则选中全选
function checkSelectAllOrNot(element1,element2){
    	var flag=0;
    	for(var i=0;i<document.getElementsByName(element1).length;i++)
    	{
    		if(document.getElementsByName(element1)[i].checked==false)
    			{
    				flag=1; //不是所有都被选中
    				break;
    			}			
    	}
    	if(flag==1){
    		document.getElementsByName(element2)[0].checked=false;
    	}
    	else{
    		document.getElementsByName(element2)[0].checked=true;
    	}
    }

//判断是否至少有一个被选中
function selectAtLeastOne(element){
    	var flag=0;
    	for(var i=0;i<document.getElementsByName(element).length;i++)
    	{
    		if(document.getElementsByName(element)[i].checked==true)
    			{
    				flag=1; //至少有一个被选中
    				break;
    			}			
    	}
    	return flag;
    }


function chechDoubleNames(titleName,modulePahseName){
	var names=document.getElementsByName(modulePahseName);
	var result=true;
	for(var i=0;i<names.length;i++){ 
		for(var j=1;j<names.length;j++){  
	if(i!=j&&names[j].value.trim()==names[i].value.trim()){
		alert("Duplicate "+titleName+", please input another one!")
	names[j].focus();
	result=false; 
	return result;
	}
	}	
	}
	return result;
}
	
function chechIsNotNullNames(titleName,modulePahseName,descName){
	 
	var names=document.getElementsByName(modulePahseName);
	var descripName=document.getElementsByName(descName);
	var result=true;
	for(var i=0;i<names.length;i++){   
	if(names[i].value.trim()==""){ 
	alert("Please input "+titleName+" Name");
	names[i].focus();
	result=false;
	return result;
	break; 
	}
	}
	for(var j=0;j<descripName.length;j++){  
		if(descripName[j].value.trim()==""){
		alert("Please input "+titleName+" Description");
		descripName[j].focus();
		result=false;
		return result;
		break; 
		}
		else if(descripName[j].value.length>300){
			alert(titleName+" Description is too long!");	
			descripName[j].focus();
			result=false;
			return result;
			break; 
		}
	}
	
	return result;
	}
function checkLength(element,length,message){
	   var name=document.getElementById(element).value;
	    if(name==null||name.trim()==""){ 
	    	return true;
	    }
	    else if(name.length>length){
	    	alert(message);
	    	document.getElementById(element).focus(); 
	    	return false;
	    }
	    return true;
	
}

function checkSelect(element,message) {

    var businessArea=document.getElementById(element); 
    if(businessArea.selectedIndex==0 ||businessArea.selectedIndex=='-1') {
       alert(message);
       document.getElementById(element).focus();
       return false;
    }
    return true;
}

//比较开始和结束日期
function compareDate(element1, element2,message){
	var startDate = document.getElementById(element1).value;
	var endDate = document.getElementById(element2).value;
	if(startDate==""){
		return false;
	}
	if(endDate==""){
		return false;
	}
	if(startDate>endDate){
		alert(message);
		return false; 
	}
	return true;
}

function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13)
          return false;
     else
          return true;
}
 
